package com.yyfax.ph.home.banner;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.List;

public class BannerModel extends Model {
	private static final String TAG = BannerModel.class.getSimpleName();

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj);
		BannerModel.AdResult adInfo = cast(obj);
		EventAgent.post(adInfo);
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		BannerModel.Service service = (BannerModel.Service) NetworkHelper.createService(BannerModel.Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.getAdInfo(token, (BannerParams) params);
	}

	public interface Service {
		@POST("sys/banner/list")
		Observable<BannerModel.AdResult> getAdInfo(@Header("token") String token, @Body BannerParams params);

	}

	public static class BannerParams{
		public String type = "3";
	}

	public static class BannerBean  implements Parcelable{
		public String imageUrl = "";
		public String calcColor = "";
		public String rcmColor = "";
		public int sort = 0;
		public String href = "";
		public String isRepost = "0"; // 是否显示分享按钮(0:否 1:是)
		public String wxTitle = ""; // 仅当isRepost为1时有意义
		public String wxContent = ""; // 仅当isRepost为1时有意义

		BannerBean(Parcel in){
			imageUrl = in.readString();
			calcColor = in.readString();
			rcmColor = in.readString();
			sort = in.readInt();
			href = in.readString();
			isRepost = in.readString();
			wxTitle = in.readString();
			wxContent = in.readString();
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(imageUrl);
			dest.writeString(calcColor);
			dest.writeString(rcmColor);
			dest.writeInt(sort);
			dest.writeString(href);
			dest.writeString(isRepost);
			dest.writeString(wxTitle);
			dest.writeString(wxContent);
		}

		public static final Creator<BannerBean> CREATOR = new Creator<BannerBean>() {
			@Override
			public BannerBean createFromParcel(Parcel in) {
				return new BannerBean(in);
			}

			@Override
			public BannerBean[] newArray(int size) {
				return new BannerBean[size];
			}
		};


	}

	public static class AdResult extends BaseResultEntity<List<BannerBean>> {

	}
}
